package com.zjitc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
import com.zjitc.dao.UserDao;
import com.zjitc.pojo.Car;
import com.zjitc.pojo.Cart;
import com.zjitc.pojo.Order;
import com.zjitc.pojo.OrderItem;
import com.zjitc.pojo.User;
import com.zjitc.service.UserService;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale.Category;
import java.util.UUID;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/19 15:01
 * @description:
 */
@Service
public class UserServiceImpl implements UserService{

  @Autowired
  private UserDao userDao;
  @Autowired
  private Jedis jedis;


   //用户登录
  @Override
  public User login(String accountnumber, String password) {

    Gson gson= new Gson();
    String user=jedis.get("user");
    System.out.println(user);
    List<User> userList=gson.fromJson(user,new TypeToken<List<User>>(){}.getType());

    for(User u:userList){
      if (u.getAccountnumber().equals(accountnumber)&&u.getPassword().equals(password)){
        return u;
      }
    }
    return null;
  }

  //注册
  @Override
  public void add(User user) {
    userDao.add(user);
    //从缓存中拿到user数据
    Gson gson= new Gson();
    String usertoString=jedis.get("user");
    List<User> userList=gson.fromJson(usertoString,new TypeToken<List<User>>(){}.getType());

    userList.add(user);

    //把userlist重新存入缓存中
    String gsonUserList=gson.toJson(userList);
    jedis.set("user",gsonUserList);

  }

  @Override
  public List<Car> findAll() {
    return userDao.findAll();
  }

  @Override
  public Car findCarId(Integer carid) {
    Gson gson= new Gson();
    String cartoString=jedis.get("car");
    List<Car> carList=gson.fromJson(cartoString,new TypeToken<List<Car>>(){}.getType());
    for(Car car:carList){
      if (carid.equals(car.getCarid())){
        return car;
      }
    }
    return null;
  }

  @Override
  public List<Cart> findCart(Integer id) {
    List<Cart> cartList1=new ArrayList<>();
    Gson gson= new Gson();
    String cartoString=jedis.get("cart");
    List<Cart> carList=gson.fromJson(cartoString,new TypeToken<List<Cart>>(){}.getType());
    for (Cart cart1:carList){
      if (String.valueOf(cart1.getId()).equals(String.valueOf(id))){
        cartList1.add(cart1);
      }
    }
    return cartList1;
  }

  @Override
  public Car findCarByCarId(Integer carid) {
    Gson gson= new Gson();
    String cartoString=jedis.get("car");
    List<Car> carList=gson.fromJson(cartoString,new TypeToken<List<Car>>(){}.getType());
    for (Car car:carList){
      if (car.getCarid()==carid){
         return car;
      }
    }
    return null;
  }

  @Override
  public void addtoCart(Cart cart) {
    Gson gson= new Gson();
    String cartoString=jedis.get("cart");
    List<Cart> carList=gson.fromJson(cartoString,new TypeToken<List<Cart>>(){}.getType());
    Cart cartItem=userDao.findByIdAndCarId(cart.getId(),cart.getCarid());
    //判断是否为空
    if (cartItem==null){
      cart.setCartid(UUID.randomUUID().toString().replace("-",""));
      //存入数据库
      userDao.addtoCart(cart);
      //加入缓存
      carList.add(cart);
      String cart1=gson.toJson(carList);
      jedis.set("cart",cart1);
    }else {
      //数据库中有此商品，则修改数量
      int num=cartItem.getNum()+cart.getNum();
      cartItem.setNum(num);
      userDao.updateCartItem(cartItem);
      for (Cart c:carList){
        if (String.valueOf(c.getCartid()).equals(String.valueOf(cartItem.getCartid()))){
          c.setNum(num);
        }
      }
      //加入缓存
      String gsonToCartItems=gson.toJson(carList);
      jedis.set("cart",gsonToCartItems);
    }
  }

  @Override
  public void deletecart(String cartid) {
    userDao.deletecart(cartid);
    //加入缓存
    Gson gson= new Gson();
    String cartoString=jedis.get("cart");
    List<Cart> carList=gson.fromJson(cartoString,new TypeToken<List<Cart>>(){}.getType());
    for(Cart cart:carList){
      if(String.valueOf(cartid).equals(String.valueOf(cart.getCartid()))){
        carList.remove(cart);
        System.out.println(carList);
        String cart1=gson.toJson(carList);
        jedis.set("cart",cart1);
        return;
      }
    }
  }

  @Override
  public void clearCart(Integer id) {
    //从数据库中删除
    userDao.clearAll(id);
    //加入缓存
    Gson gson= new Gson();
    String cartoString=jedis.get("cart");
    List<Cart> carList=gson.fromJson(cartoString,new TypeToken<List<Cart>>(){}.getType());
    List<Cart> cart=new ArrayList<>();
    for(Cart cart1:carList){
      if(String.valueOf(id).equals(String.valueOf(cart1.getId()))){
        cart.add(cart1);
      }
    }
    carList.removeAll(cart);
    String gsonToCartItems=gson.toJson(carList);
    jedis.set("cart",gsonToCartItems);
  }

  @Override
  public List<Cart> findCartItemByUid(Integer id) {
    Gson gson=new Gson();
    List<Cart> CartList=new ArrayList<>();
    String gsonCartItem=jedis.get("cart");
    List<Cart> cartItems=gson.fromJson(gsonCartItem,new TypeToken<List<Cart>>(){}.getType());
    for(Cart cart:cartItems){
      if(String.valueOf(id).equals(String.valueOf(cart.getId()))){
        CartList.add(cart);
      }
    }
    return CartList;
  }

  @Override
  public void addOrder(Order order) {
    userDao.addOrder(order);
  }

  @Override
  public void addOrderItem(OrderItem orderItem) {
    userDao.addOrderItem(orderItem);
  }

  @Override
  public List<Order> findOrderAll(Integer id) {
    List<Order> orderAll = userDao.findOrderAll(id);
    for (Order os : orderAll) {
      List<OrderItem> oidList = userDao.findOItemByOid(os.getOid());
      for (OrderItem olt : oidList) {
        Car producto = userDao.findProductByPid(olt.getCarid());
        olt.setCar(producto);
      }
      os.setOrderItemList(oidList);
    }

    return orderAll;
  }

  @Override
  public Order findOrderByOid(String oid) {
    Order order=userDao.findOrderByOid(oid);
    List<OrderItem> list=userDao.findOrderItemByOid(oid);
    for(OrderItem ot:list){
      Car car=userDao.findCid(ot.getCarid());
      ot.setCar(car);
    }
    order.setOrderItemList(list);
    return order;
  }

  @Override
  public void updateOid(String oid) {
    userDao.updateOid(oid);
  }


}
